package com.toolkit.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.context.annotation.Configuration;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

@Configuration
@Slf4j
public class FilePathUtil {

    @Value("${filepath.static}")
    public String fileRootPath;
    @Value("${filepath.downloadPath}")
    public String downloadPath;
    /**
     * 获取当前jar同级目录
     * @return
     */
    public String getFileDirect(){
        return new  ApplicationHome(getClass()).getSource().getParent() + fileRootPath;
    }
    public String getDownloadPathFileDirect(){
        return new  ApplicationHome(getClass()).getSource().getParent() + downloadPath;
    }

    /**
     * 根据文件夹名称获取对应的日期格式文件夹绝对路径（例：{pathName}/202404/15/）
     * @param pathName 文件夹名称
     * @return
     */
    public String getDateFileDirect(String pathName){
        SimpleDateFormat yyyyMM = new SimpleDateFormat("yyyyMM");
        SimpleDateFormat dd = new SimpleDateFormat("dd");
        String yearMonth = yyyyMM.format(new Date());//添加日期文件夹
        String day = dd.format(new Date());//添加日期文件夹
        String path = "";
        if (StringUtils.isEmpty(pathName)) {
            path = yearMonth + File.separator + day + File.separator;
        } else {
            path = pathName + File.separator + yearMonth + File.separator + day + File.separator;
        }
        return path;
    }
}
